package com.ruoyi.media.mpQrcode.controller;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.media.forestApi.DouyinApi;
import com.ruoyi.media.mpQrcode.domain.MediaMpQrcodeLink;
import com.ruoyi.media.mpQrcodeData.domain.MediaMpQrcodeData;
import com.ruoyi.media.mpQrcodeData.domain.bo.MediaMpQrcodeDataBo;
import com.ruoyi.media.mpQrcodeData.mapper.MediaMpQrcodeDataMapper;
import com.ruoyi.media.mpQrcodeData.service.IMediaMpQrcodeDataService;
import com.ruoyi.media.utils.WxUtils;
import com.ruoyi.media.utils.XStringUtils;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.media.mpQrcode.domain.vo.MediaMpQrcodeLinkVo;
import com.ruoyi.media.mpQrcode.domain.bo.MediaMpQrcodeLinkBo;
import com.ruoyi.media.mpQrcode.service.IMediaMpQrcodeLinkService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 微信群二维码参数
 *
 * @author dqw
 * @date 2023-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/media/mpQrcodeLink")
public class MediaMpQrcodeLinkController extends BaseController {

    private final IMediaMpQrcodeLinkService iMediaMpQrcodeLinkService;

    private final IMediaMpQrcodeDataService mediaMpQrcodeDataService;

    private final MediaMpQrcodeDataMapper mediaMpQrcodeDataMapper;


    /**
     * 查询微信群二维码参数列表
     */
    @SaCheckPermission("media:mpQrcodeLink:list")
    @GetMapping("/list")
    public TableDataInfo<MediaMpQrcodeLinkVo> list(MediaMpQrcodeLinkBo bo, PageQuery pageQuery) {
        return iMediaMpQrcodeLinkService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取微信二维码内容参数
     *
     * @param id 主键
//     */
//    @SaCheckPermission("media:mpQrcodeLink:query")
//    @GetMapping("/getContentData/{id}")
//    public R<?> getContentData(@PathVariable Long id) {
//        MediaMpQrcodeLink mediaMpQrcodeLink = iMediaMpQrcodeLinkService.getById(id);
//        return R.ok(iMediaMpQrcodeLinkService.checkQRCodeContent(mediaMpQrcodeLink));
//    }




    /**
     * 导出微信群二维码参数列表
     */
    @SaCheckPermission("media:mpQrcodeLink:export")
    @Log(title = "微信群二维码参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MediaMpQrcodeLinkBo bo, HttpServletResponse response) {
        List<MediaMpQrcodeLinkVo> list = iMediaMpQrcodeLinkService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信群二维码参数", MediaMpQrcodeLinkVo.class, response);
    }

    /**
     * 获取微信群二维码参数详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("media:mpQrcodeLink:query")
    @GetMapping("/{id}")
    public R<MediaMpQrcodeLinkVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iMediaMpQrcodeLinkService.queryById(id));
    }

    /**
     * 新增微信群二维码参数
     */
    @SaCheckPermission("media:mpQrcodeLink:add")
    @Log(title = "微信群二维码参数", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MediaMpQrcodeLinkBo bo) {
        return toAjax(iMediaMpQrcodeLinkService.insertByBo(bo));
    }

    /**
     * 修改微信群二维码参数
     */
    @SaCheckPermission("media:mpQrcodeLink:edit")
    @Log(title = "微信群二维码参数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MediaMpQrcodeLinkBo bo) {
        return toAjax(iMediaMpQrcodeLinkService.updateByBo(bo));
    }

    /**
     * 删除微信群二维码参数
     *
     * @param ids 主键串
     */
    @SaCheckPermission("media:mpQrcodeLink:remove")
    @Log(title = "微信群二维码参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMediaMpQrcodeLinkService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 根据上传的gif图片解析
     * @param file
     * @return
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> upload(@RequestPart("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String linkId =  request.getParameter("id");
        if(StringUtil.isEmpty(linkId)){
            throw new RuntimeException("id异常！");
        }
        return R.ok(iMediaMpQrcodeLinkService.saveImageQR(file.getInputStream(),linkId));
    }


}
